"""脑波数据上传相关的数据模式

定义脑波数据上传请求和响应的数据结构
用于验证APK端上传的脑波数据格式
契合云端存储接口的数据模型格式
"""

from typing import Optional, List
from pydantic import BaseModel, Field, ConfigDict


class WaveData(BaseModel):
    """波形数据结构"""
    frames: List[float] = Field(..., description="波形数据帧")


class FrequencyBandData(BaseModel):
    """频段数据结构"""
    frame_rate: Optional[int] = Field(None, description="帧率（可选）")
    wave: WaveData = Field(..., description="原始波形数据")
    filtered: WaveData = Field(..., description="过滤后的波形数据")


class EEGUploadRequest(BaseModel):
    """脑波数据上传请求模式
    
    接收APK端上传的脑波数据，包含各个频段的原始和过滤数据
    契合云端存储接口CloudStorageEEGData格式
    支持时序数据处理，解决网络传输乱序问题
    """
    
    # 基础信息字段 - 契合云端接口
    id: str = Field(..., description="会话ID")
    room_id: str = Field(..., description="房间ID，必需字段")
    
    # 时序信息字段 - 用于处理数据包顺序
    data_timestamp: float = Field(..., description="数据包开始时间戳（Unix时间戳）")
    duration_seconds: float = Field(default=10.0, description="数据包时长（秒）", ge=0, le=60.0)
    sequence_number: int = Field(..., description="数据包序号（用于排序）", ge=0)
    client_send_time: Optional[float] = Field(None, description="客户端发送时间戳（可选）")
    
    # 原始脑波数据
    eeg_wave_data: FrequencyBandData = Field(..., description="原始脑波数据")
    
    # 8个频段数据
    delta_data: FrequencyBandData = Field(..., description="Delta波段数据(0.5-4Hz)")
    theta_data: FrequencyBandData = Field(..., description="Theta波段数据(4-8Hz)")
    low_alpha_data: FrequencyBandData = Field(..., description="低Alpha波段数据(8-10Hz)")
    high_alpha_data: FrequencyBandData = Field(..., description="高Alpha波段数据(10-12Hz)")
    low_beta_data: FrequencyBandData = Field(..., description="低Beta波段数据(12-20Hz)")
    high_beta_data: FrequencyBandData = Field(..., description="高Beta波段数据(20-30Hz)")
    low_gamma_data: FrequencyBandData = Field(..., description="低Gamma波段数据(30-40Hz)")
    mid_gamma_data: FrequencyBandData = Field(..., description="中Gamma波段数据(40-50Hz)")
    
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "id": "session_12345",
                "room_id": "room_001",
                
                # 时序信息
                "data_timestamp": 1758869951.59033,
                "duration_seconds": 10.0,
                "sequence_number": 1,
                "client_send_time": 1758869961.59033,
                
                "eeg_wave_data": {
                    "frame_rate": 512,
                    "wave": {
                        "frames": [1.2, 1.5, 1.8, 2.1, 1.9]
                    },
                    "filtered": {
                        "frames": [1.1, 1.4, 1.7, 2.0, 1.8]
                    }
                },
                "delta_data": {
                    "frame_rate": 1,
                    "wave": {
                        "frames": [0.8, 0.9, 1.0, 0.7, 0.6]
                    },
                    "filtered": {
                        "frames": [0.7, 0.8, 0.9, 0.6, 0.5]
                    }
                },
                "theta_data": {
                    "frame_rate": 1,
                    "wave": {
                        "frames": [1.5, 1.6, 1.4, 1.7, 1.3]
                    },
                    "filtered": {
                        "frames": [1.4, 1.5, 1.3, 1.6, 1.2]
                    }
                },
                "low_alpha_data": {
                    "frame_rate": 1,
                    "wave": {
                        "frames": [2.1, 2.3, 2.0, 2.4, 1.9]
                    },
                    "filtered": {
                        "frames": [2.0, 2.2, 1.9, 2.3, 1.8]
                    }
                },
                "high_alpha_data": {
                    "frame_rate": 1,
                    "wave": {
                        "frames": [1.8, 2.0, 1.7, 2.1, 1.6]
                    },
                    "filtered": {
                        "frames": [1.7, 1.9, 1.6, 2.0, 1.5]
                    }
                },
                "low_beta_data": {
                    "frame_rate": 1,
                    "wave": {
                        "frames": [1.2, 1.4, 1.1, 1.5, 1.0]
                    },
                    "filtered": {
                        "frames": [1.1, 1.3, 1.0, 1.4, 0.9]
                    }
                },
                "high_beta_data": {
                    "frame_rate": 1,
                    "wave": {
                        "frames": [0.9, 1.1, 0.8, 1.2, 0.7]
                    },
                    "filtered": {
                        "frames": [0.8, 1.0, 0.7, 1.1, 0.6]
                    }
                },
                "low_gamma_data": {
                    "frame_rate": 1,
                    "wave": {
                        "frames": [0.5, 0.7, 0.4, 0.8, 0.3]
                    },
                    "filtered": {
                        "frames": [0.4, 0.6, 0.3, 0.7, 0.2]
                    }
                },
                "mid_gamma_data": {
                    "frame_rate": 1,
                    "wave": {
                        "frames": [0.3, 0.5, 0.2, 0.6, 0.1]
                    },
                    "filtered": {
                        "frames": [0.2, 0.4, 0.1, 0.5, 0.0]
                    }
                }
            }
        }
    )


class EEGUploadResponse(BaseModel):
    """脑波数据上传响应模式
    
    契合云端存储接口的响应格式
    """
    
    success: bool = Field(..., description="上传是否成功")
    message: str = Field(..., description="响应消息")
    data: dict = Field(..., description="响应数据")
    
    model_config = ConfigDict(
        json_schema_extra={
            "example": {
                "success": True,
                "message": "数据上传成功",
                "data": {
                    "id": "session_12345",
                    "uploaded_at": "2023-11-04T12:34:56Z",
                    "data_points": 5120
                }
            }
        }
    )